<template>
	<view class="zg-area">
		<view class="area-nav" :style="{'padding-top': statusbar_height + 'px'}">
			<view class="nav">
				<view class="nav-left" v-if="isRoutes" @click="navBack">
					<image src="/pagesShop/static/left.svg" mode=""></image>
				</view>
				<view class="nav-left" v-else @click="pageJumps('/pages/tabbar/home', true)">
					<uni-icons class="" type="home" size="24" color="#fff"></uni-icons>
				</view>
				<view class="nav-center">
					<input type="text" placeholder="商品/关键词" v-model="keyword" @confirm="getList1" confirm-type="搜索">
					<image src="/pagesShop/static/search2.png" mode=""></image>
				</view>
				<view class="nav-right"></view>
			</view>
		</view>
		<view class="fixed-tool-top" :style="{'top': statusbar_height + 44 + 'px'}">
			<!-- 分类1 -->
			<view class="fl1">
				<scroll-view scroll-x style="height: 40px">
					<view class="fltab">
						<view class="fltab-item fltab-item1" :class="{active: currentIndex1 === index}" v-for="(item,index) in nav1" :key="index"
						 @click="changeTab1(item,index)">
							{{item.name}}
						</view>
					</view>
				</scroll-view>
			</view>
			<!-- 分类2 -->
			<!-- <view class="fl2">
				<scroll-view scroll-x style="height: 40px">
					<view class="fltab">
						<view class="fltab-item" :class="{active: currentIndex2 === index}" v-for="(item,index) in nav2" :key="index" @click="changeTab2(item,index)">
							{{item.name}}
						</view>
					</view>
				</scroll-view>
			</view> -->
			<!-- 筛选 -->
			<view class="sx" >
				<scroll-view scroll-x style="height: 40px">
					<view class="sx-box">
						<view class="sx-box-item" :class="{active: currentIndex3 === 0}" @click="changeTab3(0)">
							<text>综合</text>
						</view>
						<view class="sx-box-item" :class="{active: currentIndex3 === 1}" @click="changeTab3(1)">
							<text>销量</text>
							<!-- <view class="sx-box-icon">
								<template v-if="currentIndex3 === 1">
									<image src="/pagesShop/static/sf2-1.png" mode="widthFix"></image>
									<image src="/pagesShop/static/sf1-1.png" mode="widthFix"></image>
								</template>
								<template v-else>
									<image src="/pagesShop/static/sf2.png" mode="widthFix"></image>
									<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
								</template>
							</view> -->
						</view>
						<view class="sx-box-item" :class="{active: currentIndex3 === 2}" @click="changeTab3(2)">
							<text>价格</text>
							<view class="sx-box-icon">
								<template v-if="currentIndex3 === 2">
									<template v-if="sortx === 1">
										<image src="/pagesShop/static/sf2.png" mode="widthFix"></image>
										<image src="/pagesShop/static/sf1-1.png" mode="widthFix"></image>
									</template>
									<template v-else>
										<image src="/pagesShop/static/sf2-1.png" mode="widthFix"></image>
										<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
									</template>
								</template>
								<template v-else>
									<image src="/pagesShop/static/sf2.png" mode="widthFix"></image>
									<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
								</template>
							</view>
						</view>
						
						<view class="sx-box-item" @click="changeTab5">
							<text>筛选</text>
							<view class="sx-box-icon">
								<image class="sbi1" src="/pagesShop/static/ff.png" mode="widthFix"></image>
							</view>
						</view>
						<view class="sx-box-item" @click="changeTab4(row_type)">
							<image v-if="row_type === 'row'" src="/pagesShop/static/f1.png" mode="widthFix"></image>
							<image v-else src="/pagesShop/static/f2.png" mode="widthFix"></image>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
		<view class="content" :style="{'padding-top': statusbar_height + 124 + 'px'}">
			<view class="area-1" v-if="list.length > 0">
				<view v-if="row_type === 'row'">
					<view class="card" v-for="(item,index) in list" :key="index" @click="goRouter1(item)">
						<view class="card-left">
							<image class="tp-br8" :src="`${shopImgUrl}${item.pic}`" mode="aspectFill"></image>
						</view>
						<view class="card-right">
							<view class="card-title ellispsis1">
								{{item.name}}
							</view>
							<view class="card-info" style="margin-top: 20rpx;">
								<view class="card-price">
									<text class="flex align_baseline tp-fz24">￥ <text class="tp-fz36">{{item.price}}</text></text>
								</view>
								<view class="card-xl">
									销量{{handleSales(item.sale)}}
								</view>
							</view>
							<view class="card-info">
								<view class="card-score">
									<text>积分可抵扣 {{item.jfDeductionAmount}} 元</text>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="card-wapper" v-else>
					<view class="card" v-for="(item,index) in list" :key="index" @click="goRouter1(item)">
						<view class="card-top">
							<image :src="`${shopImgUrl}${item.pic}`" mode="widthFix"></image>
						</view>
						<view class=" tp-w100 tp-pd20 tp-bbox">
							<view class="card-title ellispsis1">
								{{item.name}}
							</view>
							<view class="card-info">
								<view class="card-price">
									<text class="flex align_baseline tp-fz24">￥ <text class="tp-fz34">{{item.price}}</text></text>
								</view>
								<view class="card-xl">
									销量{{handleSales(item.sale)}}
								</view>
							</view>
							<view class="card-info">
								<view class="card-score">
									<text>积分可抵扣 {{item.jfDeductionAmount}} 元</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="nomore" v-if="list.length === 0 && !loading">
				<view class="nomore-text">
					<image src="/pagesShop/static/zanwu.png" mode="widthFix"></image>
					<view>暂无数据</view>
				</view>
			</view>
			<view class="uni-loadmore" v-if="showLoadMore">{{loadMoreText}}</view>
		</view>
		<!-- 筛选 -->
		<view class="filter-wapper" v-if="show1" @touchmove.stop.prevent>
			<view class="filter-extra" @click="show1=false"></view>
			<view class="filter-content">
				<view class="filter-box">
					<view class="filter-h4" v-if="nav2.length">分类</view>
					<view class="" v-if="nav2.length">
						<text @click="categoryId.id == item.id ? categoryId = {} : categoryId = item" class="tp-border1 tp-ptb0lr20 tp-h56 tp-lh54 tp-br4 tp-bgf tp-fc666 tp-fz26 tp-dib tp-mr20 tp-mb20"
						 :class="{'classActive': categoryId.id === item.id}" v-for="(item,index) in nav2" :key="index">{{item.name}}</text>
					</view>
					<view class="filter-h4">价格区间(元)</view>
					<view class="filter-con">
						<view class="filter-inputs">
							<input type="text" placeholder="最低价" v-model="price1">
							<text>——</text>
							<input type="text" placeholder="最高价" v-model="price2">
						</view>
					</view>
					<view class="filter-h4">好评率</view>
					<view class="filter-con">
						<view class="filter-tags">
							<view class="filter-tag" :class="{'active': good === 0}" @click="selectGoods(0)">
								100%
							</view>
							<view class="filter-tag" :class="{'active': good === 1}" @click="selectGoods(1)">
								99%以上
							</view>
							<view class="filter-tag" :class="{'active': good === 2}" @click="selectGoods(2)">
								98%以上
							</view>
							<view class="filter-tag" :class="{'active': good === 3}" @click="selectGoods(3)">
								97%以上
							</view>
						</view>
					</view>
					<!-- <view class="filter-h4">区域</view>
					<view class="filter-con">
						<view class="filter-selects">
							<view class="filter-select" @click="showMulLinkageThreePicker">
								<text>{{address[0] || '请选择省'}}</text>
								<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
							</view>
							<view class="filter-select" @click="showMulLinkageThreePicker">
								<text>{{address[1] || '请选择市'}}</text>
								<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
							</view>
							<view class="filter-select" @click="showMulLinkageThreePicker">
								<text>{{address[2] || '请选择区'}}</text>
								<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
							</view>
						</view>
					</view> -->
				</view>
				<view class="filter-btns">
					<button plain @click="resetFilter">重置</button>
					<button plain @click="confirmFilter">确认</button>
				</view>
			</view>
		</view>
		<mpvue-city-picker :themeColor="themeColor" ref="mpvueCityPicker" :pickerValueDefault="cityPickerValueDefault"
		 @onCancel="onCancel" @onConfirm="onConfirm"></mpvue-city-picker>
	</view>
</template>

<script>
	// import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue'; // 三级城市选择联动
	// import cityData from '@/common/city.data.js';

	export default {
		// components: {
		// 	mpvueCityPicker
		// },
		data() {
			return {
				price1: '', // 最低价
				price2: '', // 最高价
				good: '', // 好评
				// address: [],
				keyword: '',
				show1: false,
				row_type: 'table',
				currentIndex1: 0,
				currentIndex2: 0,
				currentIndex3: 0,
				firstCategoryId: '', // 一级分类id
				categoryId: {}, //二级分类
				pageNum: 1,
				last_page: null, // 总页数
				loadMoreText: "加载中...",
				showLoadMore: false, // 加载更多
				status: 'more',
				loading: false,
				list: [],
				nav1: [],
				nav2: [],
				// 筛选
				filters: {
					price1: '', // 最低价
					price2: '', // 最高价
					good: '', // 好评
					// address: [],
				},
				// 三级联动
				// pickerText: '', // 三级联动得到的值
				// pickerViewText: {}, // 页面需要的值 
				// province: '', // 省
				// city: '', // 市
				// area: '', // 区
				// mulLinkageTwoPicker: cityData, // 导入的数据
				// cityPickerValueDefault: [0, 0, 1], // 三级联动选取的值
				// themeColor: '#007AFF', // 确定的主题色
				sortx: 1,
				
				isRoutes: true, //  有上页 ，不是直接进的详情页
			}
		},
		onLoad() {
			let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
			if(routes.length == 1){
				this.isRoutes = false
				console.log(routes);
			}else{
				this.isRoutes = true
			}
			this.getList2()
		},
		onShow() {
			this.pageNum = 1
			this.getList1()
		},
		onPullDownRefresh() {
			this.pageNum = 1
			this.getList1()
		},
		onReachBottom() {
			this.showLoadMore = true;
			if (this.pageNum < this.last_page) {
				this.pageNum++
				this.getList1()
			} else {
				this.loadMoreText = "已没有更多数据!"
			}
		},
		methods: {
			// 根据条件查询商品
			getList1() {
				this.loading = true
				this.$http.post(this.$api.shop_list_4, {}, {
					params: {
						keyword: this.keyword,
						firstCategoryId: this.firstCategoryId, // 商品一级分类id
						categoryId: this.categoryId.id || '', // 商品二级分类id
						pageNum: this.pageNum,
						status: this.currentIndex3, // 0综合 1销量 2价格
						sort: this.sortx, // 0升序 1降序
						floorPrice: this.price1, // 最低价
						highPrice: this.price2, // 	最高价
						praiseRate: this.good, // 	好评率 0=100% 1=99% 2=100% 3=98% 3=97%
					}
				}).then(result => {
					this.loading = false
					this.showLoadMore = false;
					uni.stopPullDownRefresh()
					if (result.code === 200) {
						this.last_page = result.data.pageTotal
						const list = result.data.result;
						if (Array.isArray(list) && list.length > 0) {
							if (this.pageNum > 1) {
								this.list = this.list.concat(list);
							} else {
								this.list = list
							}
						} else {
							this.list = []
						}
					} else {
						this.list = []
					}
				}).catch(() => {
					this.loading = false
					this.list = []
					this.status = 'noMore'
				})
			},

			// 获取商品全部类目
			getList2() {
				this.$http.post(this.$api.shop_tabs_1).then(result => {
					if (result.code === 200) {
						if (result.data.length) {
							result.data.unshift({
								name: '全部',
								id: ''
							})
						}
						this.nav1 = result.data;
						if (this.nav1.length > 0 && Array.isArray(this.nav1[0].mallProductCategories)) {
							this.firstCategoryId = this.nav1[0].id
							// this.nav2 = this.nav1[0] ? [{name: '全部'}].concat(this.nav1[0].mallProductCategories) : []
						}
					}
				})
			},
			resetFilter() {
				this.price1 = '' // 最低价
				this.price2 = '' // 最高价
				this.good = '' // 好评
				this.categoryId = {};
				this.filters = {
					price1: '', // 最低价
					price2: '', // 最高价
					good: '', // 好评
				}
				this.confirmFilter()
			},
			confirmFilter() {
				this.filters = {
					price1: this.price1, // 最低价
					price2: this.price1, // 最高价
					good: this.good, // 好评
					// address: this.address,
				}
				this.show1 = false
				this.getList1()
			},
			// 选择好评率
			selectGoods(good) {
				this.good = good
			},
			changeTab1(item, index) {
				this.currentIndex1 = index
				this.firstCategoryId = item.id
				if (Array.isArray(this.nav1[index].mallProductCategories)) {
					this.nav2 = this.nav1[index].mallProductCategories || []
				}
				this.pageNum = 1
				this.changeTab2('', 0)
			},
			changeTab2(item, index) {
				this.currentIndex2 = index
				this.categoryId = index > 0 ? item.id : ''
				this.getList1()
			},
			changeTab3(index) {
				this.currentIndex3 = index
				if (index === 2) this.sortx = this.sortx === 1 ? 0 : 1
				this.getList1()
			},
			changeTab4(row_type) {
				this.row_type = row_type === 'row' ? 'table' : 'row'
			},
			// 打开筛选条件弹窗
			changeTab5() {
				this.show1 = true
			},
			// 去商品详情
			goRouter1(item) {
				if (Object.prototype.toString.call(item) === '[object Object]' && typeof item.id === 'string' && item.id !== '') {
					uni.navigateTo({
						url: `/pagesShop/details/jf_details?id=${item.id}`
					});
				} else {
					console.error("积分商品id错误",item.id);
				}
			},
			// 取消时的停靠的数据,下次打开在这个位置
			// onCancel(e) {
			// 	console.log(e)
			// },
			// // 三级联动选择
			// showMulLinkageThreePicker() {
			// 	this.$refs.mpvueCityPicker.show()
			// },
			// // 处理联动数据
			// onConfirm(e) {
			// 	this.pickerText = JSON.stringify(e)
			// 	this.pickerViewText = JSON.parse(this.pickerText)
			// 	this.address = this.pickerViewText.label.split('-')
			// },
		}
	}
</script>

<style lang="scss" scoped>
	.filter-selects {
		display: flex;
		flex-wrap: wrap;
	}

	.filter-select {
		background-color: #ebebeb;
		display: flex;
		justify-content: space-between;
		align-items: center;
		color: #999;
		width: 200rpx;
		height: 60rpx;
		padding: 0 20rpx;
		margin: 0 20rpx 20rpx 0;

		image {
			width: 20rpx;
			height: auto;
		}
	}

	.filter-inputs {
		display: flex;
		align-items: center;

		input {
			background-color: #ebebeb;
			height: 60rpx;
			border-radius: 40rpx;
			text-align: center;
			font-size: 30rpx;
		}

		text {
			margin: 0 10rpx;
			color: #ccc;
		}
	}

	.filter-tags {
		display: flex;
		flex-wrap: wrap;
	}

	.filter-tag {
		border: 1px solid #ddd;
		font-size: 26rpx;
		padding: 5rpx 10rpx;
		flex-shrink: 0;
		margin: 0 20rpx 20rpx 0;
		border-radius: 4rpx;

		&.active {
			background-color: $uni-color-error;
			color: #fff;
			border: 0;
		}
	}

	.filter-wapper {
		position: fixed;
		top: 0;
		left: 0;
		z-index: 1;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		justify-content: flex-end;
	}

	.filter-extra {
		flex-grow: 1;
		height: 100%;
	}

	.filter-content {
		width: 80%;
		height: 100%;
		background-color: #fff;
		box-sizing: border-box;
		padding: calc(var(--status-bar-height) + 44px) 30rpx 0;
		display: flex;
		flex-direction: column;
	}

	.filter-h4 {
		font-size: 30rpx;
		margin: 30rpx 0;
	}

	.filter-box {
		flex-grow: 1;
	}

	.filter-btns {
		height: 60px;
		display: flex;
		justify-content: center;
		align-items: center;

		button {
			width: 200rpx;
			height: 80rpx;
			background-color: #ebebeb;
			font-size: 30rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			margin: 0;
			border-radius: 0;

			&:nth-of-type(1) {
				border-radius: 40rpx 0 0 40rpx;
			}

			&:nth-of-type(2) {
				border-radius: 0 40rpx 40rpx 0;
				background-color: $uni-color-error;
				color: #fff;
			}
		}
	}

	.nav-center {
		text-align: left !important;
		padding: 0 !important;
		position: relative;

		input {
			width: 450rpx;
			background-color: #fff;
			color: #333;
			height: 30px;
			border-radius: 30px;
			box-sizing: border-box;
			padding: 0 20rpx 0 70rpx;
			font-size: 26rpx;
		}

		image {
			width: 16px;
			height: 16px;
			position: absolute;
			left: 12px;
			top: 8px;
		}
	}

	.card-info-right {
		display: flex;

		&>view {
			margin-left: 10rpx;
		}
	}

	.card {
		margin: 30rpx;
		padding: 20rpx;
		background-color: #fff;
		border-radius: 10rpx;
		display: flex;
		align-items: center;
	}

	.card-left {
		flex-shrink: 0;
		width: 180rpx;
		height: 180rpx;
		overflow: hidden;
		margin-right: 20rpx;

		image {
			width: 180rpx;
			height: 180rpx;
		}
	}

	.card-right {
		flex-grow: 1;
	}

	.card-title {
		font-size: 28rpx;
	}

	.card-info {
		margin-top: 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.card-price {
		text {
			&:nth-of-type(1) {
				color: $uni-color-error;
				font-weight: bold;
			}

			&:nth-of-type(2) {
				margin-left: 10rpx;
				color: #999;
				text-decoration: line-through;
			}
		}
	}

	.card-xl {
		font-size: 24rpx;
		color: #999;
	}

	.card-tools {
		margin-top: 10rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.card-time {
		margin-top: 20rpx;
		color: $uni-color-error;
		font-size: 24rpx;
	}

	.card-wapper {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		margin: 30rpx;

		.card {
			margin: 0 0 30rpx;
			padding: 0;
			flex-direction: column;
			width: 330rpx;
		}

		.card-tools {
			flex-direction: column;
			align-items: flex-start;
			margin-bottom: 10rpx;
		}

		.card-info {
			margin-top: 10rpx;
		}

		.card-time {
			margin-top: 10rpx;
		}
	}

	.card-top {
		width: 330rpx;
		height: 330rpx;

		image {
			border-radius: 12rpx 12rpx 0 0;
		}
	}

	// . {
	// 	padding: 10rpx;
	// }

	.card-score {
		font-size: 24rpx;
		background-color: #ffeeee;
		color: $uni-color-error;
		border-radius: 6rpx;
		padding: 0 10rpx;
	}

	.fixed-tool-top {
		position: fixed;
		top: calc(var(--status-bar-height) + 44px);
		left: 0;
		z-index: 1;
		width: 100%;
		height: 80px;
		box-sizing: border-box;
		background-color: #fff;
	}

	.fl1,
	.fl2 {
		border-bottom: 1rpx solid #eee;
		box-sizing: border-box;
	}

	.fltab {
		height: 100%;
		display: flex;
	}

	.fltab-item {
		display: flex;
		align-items: center;
		flex-shrink: 0;
		height: 100%;
		font-size: 26rpx;
		padding: 0 20rpx;
		position: relative;

		&.active {
			color: $blue;

			&::after {
				display: block;
			}
		}
	}

	.fltab-item1 {
		&::after {
			content: "";
			display: none;
			position: absolute;
			bottom: 0;
			left: 50%;
			margin-left: -0.5em;
			width: 1em;
			height: 6rpx;
			background-color: $blue;
		}

		&.active {
			&::after {
				display: block;
			}
		}
	}

	.sx-box {
		height: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background-color: #fff;
	}

	.sx-box-item {
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-grow: 1;

		&>image {
			width: 36rpx;
			height: auto;
		}

		&.active {
			color: $blue;
		}
	}

	.sx-box-icon {
		width: 30rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: flex-end;

		image {
			width: 20rpx;
			height: auto;

			&:nth-of-type(1) {
				margin-bottom: 4rpx;
			}
		}

		.sbi1 {
			width: 25rpx;
		}
	}

	.content {
		padding: calc(var(--status-bar-height) + 164px) 0 0;
	}

	.classActive {
		background: $uni-color-error;
		color: #FFFFFF;
	}
</style>
